import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);

import localCache from "@/utils/cache";

import { login,gettoken } from "@/api/login";
// import { getMenuList } from "@/api/menu";
import { getRoleList } from "@/api/role/role";
import { storeList } from "@/api/store";
import { getUserInfo } from "@/api/user";
// 获取菜单列表
import { getMenuList } from "@/api/menu"

import { Message } from "element-ui";
//扩展管理
import expend from  './modules/expend'
import getters from "./getters"

import router from "@/router";

const store = new Vuex.Store({
  state: {
    token: "",
    // token: localCache.getCache('token'),
    // meunlist:  JSON.parse(window.sessionStorage.getItem('menulist')) || [],//菜单
    meunlist:localCache.getCache('menulist') || [],//菜单
    userInfo: {},
    menus: [],
    roles: [],
    stores: [],
    caozuoinfo:{},
    active:window.sessionStorage.getItem('active') || 'main/getCountData',
  },
  mutations: {
    changeToken(state, token) {
      state.token = token;
    },
    changeUserInfo(state, userInfo) {
      state.userInfo = userInfo;
    },
    changeMenus(state, menu) {
      state.menus = menu;
    },
    changeRoles(state, role) {
      state.roles = role;
    },
    changeStores(state, store) {
      state.stores = store;
    },
    //操作详情
    handelacaouoinfo(state,data){
      state.caozuoinfo = data
    },
    //把菜单存入vuex
    setmenulist(state,data){
      state.meunlist = data
      // window.sessionStorage.setItem('menulist',JSON.stringify(data))
      localCache.setCache('menulist',data)
  },
  //菜单选中状态
  setactive(state,data){
    console.log(1);
    state.active=data
    // localCache.setCache('isactive',data)
    window.sessionStorage.setItem('active',data)
  },
  //初始化菜单
  setmenulist1(state,data){
    state.meunlist = data
    // window.sessionStorage.setItem('menulist',JSON.stringify(data))
    localCache.setCache('menulist',data)
  },
  },
  actions: {
    async loginAction({ commit }, payload) {
      //1. 登录操作
      // 获取token
      const datatoken  =  await gettoken()
      //登录
        const loginInfo = await login(payload)
        console.log(loginInfo.data.auth_token);
        if (loginInfo.code === 200) {
        commit("changeToken", loginInfo.data.auth_token);
        localCache.setCache("token", loginInfo.data.auth_token);
        Message.success("登录成功!");
        router.push("/");
      } else {
        return Message.error(loginInfo.msg);
      }
      //获取用户信息
      const userInfo = await getUserInfo();
      if (userInfo.code === 1) {
        commit("changeUserInfo", userInfo.data);
        localCache.setCache("userInfo", userInfo.data);
      }
      // 获取菜单信息
      const menuPagination= {
           page: 1,
           limit: 100,
           name: "",
           type: 0,
      }
      commit('setactive','main/getCountData')
      // window.sessionStorage.setItem('active','main/getCountData') //选中状态
      const { data: menuList } = await getMenuList(menuPagination);
        menuList.forEach(item=>{
        item.val = true
      })
       menuList[0].val =false
       commit("setmenulist", menuList );
      //获取菜单信息
      // const menuInfo = await getMenuList({
      //   page: 1,
      //   limit: 1000,
      //   type: 0,
      //   name: "",
      //   auth_token: loginInfo.data.auth_token
      // });
      // if (menuInfo.code === 1) {
      //   commit("changeMenus", menuInfo.data);
      //   localCache.setCache("menus", menuInfo.data);
      // }

      // const roleInfo = await getRoleList({
      //   page: 1,
      //   limit: 1000,
      //   name: "",
      //   type: 0,
      //   auth_token: loginInfo.data.auth_token
      // });
      // if (roleInfo.code === 1) {
      //   commit("changeRoles", roleInfo.data);
      //   localCache.setCache("roles", roleInfo.data);
      // }

      // const storeInfo = await storeList({
      //   page: 1,
      //   limit: 1000,
      //   name: "",
      //   auth_token: loginInfo.data.auth_token
      // });
      // if (storeInfo.code === 1) {
      //   commit("changeStores", storeInfo.data);
      //   localCache.setCache("stores", storeInfo.data);
      // }
    },
    //初始化菜单
   async getmenulist(commit){
      const menuPagination= {
        page: 1,
        limit: 100,
        name: "",
        type: 0,
   }
       const { data: menuList } = await getMenuList(menuPagination);
        menuList.forEach(item=>{
        item.val = true
      })
      menuList[0].val =false
      window.sessionStorage.setItem('active','main/getCountData') //选中状态
      this.commit("setmenulist", menuList );

    },

    loadLocalState({ commit }) {
      const token = localCache.getCache("token");
      if (token) {
        commit("changeToken", token);
      }
      const userInfo = localCache.getCache("userInfo");
      if (userInfo) {
        commit("changeUserInfo", userInfo);
      }
      const menuList = localCache.getCache("menus");
      if (menuList) {
        commit("changeMenus", menuList);
      }
      const roleList = localCache.getCache("roles");
      if (roleList) {
        commit("changeRoles", roleList);
      }
      const storeList = localCache.getCache("stores");
      if (storeList) {
        commit("changeStores", storeList);
      }
    },
  },
  modules:{
    expend
  },
  getters
});

export function setupState() {
  store.dispatch("loadLocalState");
}

export default store;
